/**  
* @Title: UserController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月17日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrSysRule;
import com.lemon.model.cityRun.CrSysRuleGroup;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrSysRuleService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: UserController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月17日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/crus")
public class CrShopUserController {

	@Resource
	private CrShopUserService userService;
	@Resource
	private CrSysRuleService ruleService;

	//获取全部用户信息
	@LoginRequired
	@RequestMapping("/getUsRule")
	public ReqResult getRuleList(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			CrShopUser u = userService.selectShopUserByAcc(user.getUserAccount());
			if (IdTypeHandler.decode(u.getRuleId()) == 1) {
				CrSysRule rule = new CrSysRule();
				rule.setOrderBy(" order by sort asc");
				rule.setStatus(1);
				rule.setPageSize(10000);
				List<CrSysRule> list2 = ruleService.selectListBySelective(rule);
				reqResult.setReturnResult(list2);
			} else {
				CrShopUser list = userService.selectShopUserByAcc(u.getUserAccount());
				CrSysRuleGroup primaryKey = ruleService.selectGroupByPrimaryKey(list.getRuleId());
				String ru = primaryKey.getRuleIds();

				String[] ruleIds = ru.split(",");
				String idd = "";
				for (int j = 0; j < ruleIds.length; j++) {
					long id = IdTypeHandler.decode(ruleIds[j]);
					idd += id;
					if (j != ruleIds.length - 1) {
						idd += ",";
					}
				}
				CrSysRule rule = new CrSysRule();
				rule.setStatus(1);
				rule.setWhere(" and id in(" + idd + ") ");
				rule.setOrderBy(" order by sort asc");
				rule.setPageSize(500);
				List<CrSysRule> list2 = ruleService.selectListBySelective(rule);

				list.setUserPwd("");
				reqResult.setReturnResult(list2);
			}
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//获取全部用户信息
	@LoginRequired
	@RequestMapping("/getUserList")
	public ReqResult getUserList(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			long ri = IdTypeHandler.decode(u.getRuleId());
			/*
			 * if(ri == 0 || ri == 2) {
			 * reqResult.setReturnResult("权限不足",ReqResult.code_no_permission);
			 * return reqResult;
			 * }
			 */
			if (user.getUserType() == 1) {
				user.setWhere("  and a.user_status != 0 ");
				//查询管理员列表
				List<CrShopUser> list = userService.selectAdminListBySelective(user);
				reqResult.setReturnResult(list);
			} else {
				//查询客户列表
				user.setWhere("  and a.user_status != 0 ");
				//查询管理员列表
				List<CrShopUser> list = userService.selectShopListBySelective(user);
				reqResult.setReturnResult(list);
			}
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	//获取全部用户信息
	@LoginRequired
	@RequestMapping("/getUserById")
	public ReqResult getUserById(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			long ri = IdTypeHandler.decode(u.getRuleId());
			//			if (ri == 0 || ri == 2) {
			//				reqResult.setReturnResult("权限不足" , ReqResult.code_no_permission);
			//				return reqResult;
			//			}
			if (1 == user.getUserType()) {
				user.setWhere("  and a.user_status != 0 ");
				//查询管理员列表
				List<CrShopUser> list = userService.selectAdminListBySelective(user);
				reqResult.setReturnResult(list.size()<=0 ? null : list.get(0));
			} else {
				if (0 == user.getUserType()) {
					user.setUserType(2);
				}
				//查询客户列表
				user.setWhere("  and user_status != 0 ");
				List<CrShopUser> list = userService.selectShopListBySelective(user);

				reqResult.setReturnResult(list.size() <= 0 ? null : list.get(0));
			}
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 获取店铺基本信息
	 * </blockquote></b>
	 * 
	 * @Title: getUserInfoById
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月22日
	 */
	@RequestMapping("/getUserInfoById")
	public ReqResult getUserInfoById(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser cr = userService.selectById(user.getId());
			cr.setUserPwd(null);
			cr.setUserIdCard(null);
			reqResult.setReturnResult(cr);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 新增管理员信息
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/addAdminUserInfo.do" , method = RequestMethod.POST)
	public ReqResult addAdminUserInfo(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			long ri = IdTypeHandler.decode(u.getRuleId());
			if (ri == 0 || ri == 2) {
				reqResult.setReturnResult("权限不足" , ReqResult.code_no_permission);
				return reqResult;
			}

			CrShopUser acc = userService.selectShopUserByAcc(user.getUserAccount());
			if (acc != null) {
				reqResult.setReturnResult("当前账号已存在" , ReqResult.code_exist);
				return reqResult;
			}
			user.setUserType(1);
			user.setUserStatus(1);
			user.setUserName(user.getUserAccount());
			user.setUserPwd(user.getUserPwd().toUpperCase());
			int row = userService.insertAdminUserInfo(user);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 新增管理员信息
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/updateAdminUserInfo.do" , method = RequestMethod.POST)
	public ReqResult updateAdminUserInfo(@ModelAttribute CrShopUser user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String ids = request.getParameter("remAllIds");
			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			long ri = IdTypeHandler.decode(u.getRuleId());
			//			if (ri == 0 || ri == 2) {
			//				reqResult.setReturnResult("权限不足" , ReqResult.code_no_permission);
			//				return reqResult;
			//			}
			//			if (StringUtil.isNotBlank(user.getUserAccount())) {
			//				CrShopUser acc = userService.selectShopUserByAcc(user.getUserAccount());
			//				if (acc != null) {
			//					reqResult.setReturnResult("当前账号已存在" , ReqResult.code_exist);
			//					return reqResult;
			//				}
			//			}
			if (StringUtil.isNotBlank(ids)) {
				CrShopUser u2 = new CrShopUser();
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					u2.setUserStatus(0);
					u2.setId(id);
					int row = userService.updateAdminUserInfo(u2);
					reqResult.setReturnResult(row);
				}
			} else {
				int row = userService.updateAdminUserInfo(user);
				reqResult.setReturnResult(row);
			}

		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

}
